// server.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

// 存储所有连接的客户端
const clients = [];

wss.on('connection', (ws) => {
    clients.push(ws);
    console.log('新客户端连接，总数:', clients.length);

    // 广播消息给所有客户端（简化版，实际需配对逻辑）
    ws.on('message', (message) => {
        clients.forEach(client => {
            if (client !== ws && client.readyState === WebSocket.OPEN) {
                client.send(message);
            }
        });
    });

    ws.on('close', () => {
        clients.splice(clients.indexOf(ws), 1);
    });
});

console.log('信令服务器运行在 ws://localhost:8080');